/**
数据验证（表单验证）
来自 grace.hcoder.net
*/
var _ = require('lodash')
module.exports = {
  error: '',
  check: function(data, rule) {
    for (var i = 0; i < rule.length; i++) {
      if (!rule[i].checkType) { return true }
      if (!rule[i].name) { return true }
      if (!rule[i].errorMsg) { return true }
      var reg = ''
      var minMax = []
      switch (rule[i].checkType) {
        case 'string':
          reg = new RegExp('^.{' + rule[i].checkRule + '}$')
          if (!reg.test(_.get(data, rule[i].name))) { this.error = rule[i].errorMsg; return false }
          break
        case 'int':
          reg = new RegExp('^(-[1-9]|[1-9])[0-9]{' + rule[i].checkRule + '}$')
          if (!reg.test(_.get(data, rule[i].name))) { this.error = rule[i].errorMsg; return false }
          break
        case 'between':
          if (!this.isNumber(_.get(data, rule[i].name))) {
            this.error = rule[i].errorMsg
            return false
          }
          minMax = rule[i].checkRule.split(',')
          minMax[0] = Number(minMax[0])
          minMax[1] = Number(minMax[1])
          if (_.get(data, rule[i].name) > minMax[1] || _.get(data, rule[i].name) < minMax[0]) {
            this.error = rule[i].errorMsg
            return false
          }
          break
        case 'betweenD':
          reg = /^-?[1-9][0-9]?$/
          if (!reg.test(_.get(data, rule[i].name))) { this.error = rule[i].errorMsg; return false }
          minMax = rule[i].checkRule.split(',')
          minMax[0] = Number(minMax[0])
          minMax[1] = Number(minMax[1])
          if (_.get(data, rule[i].name) > minMax[1] || _.get(data, rule[i].name) < minMax[0]) {
            this.error = rule[i].errorMsg
            return false
          }
          break
        case 'betweenF':
          reg = /^-?[0-9][0-9]?.+[0-9]+$/
          if (!reg.test(_.get(data, rule[i].name))) { this.error = rule[i].errorMsg; return false }
          minMax = rule[i].checkRule.split(',')
          minMax[0] = Number(minMax[0])
          minMax[1] = Number(minMax[1])
          if (_.get(data, rule[i].name) > minMax[1] || _.get(data, rule[i].name) < minMax[0]) {
            this.error = rule[i].errorMsg
            return false
          }
          break
        case 'same':
          if (_.get(data, rule[i].name) !== rule[i].checkRule) { this.error = rule[i].errorMsg; return false }
          break
        case 'notsame':
          if (_.get(data, rule[i].name) === rule[i].checkRule) { this.error = rule[i].errorMsg; return false }
          break
        case 'email':
          reg = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
          if (!reg.test(_.get(data, rule[i].name))) { this.error = rule[i].errorMsg; return false }
          break
        case 'phoneno':
          reg = /^1[0-9]{10,10}$/
          if (!reg.test(_.get(data, rule[i].name))) { this.error = rule[i].errorMsg; return false }
          break
        case 'zipcode':
          reg = /^[0-9]{6}$/
          if (!reg.test(_.get(data, rule[i].name))) { this.error = rule[i].errorMsg; return false }
          break
        case 'reg':
          reg = new RegExp(rule[i].checkRule)
          if (!reg.test(_.get(data, rule[i].name))) { this.error = rule[i].errorMsg; return false }
          break
        case 'in':
          if (rule[i].checkRule.indexOf(_.get(data, rule[i].name)) === -1) {
            this.error = rule[i].errorMsg; return false
          }
          break
        case 'otherCheck': // otherRule属性为true 才对其校验
          reg = new RegExp(rule[i].checkRule)
          if (rule[i].otherRule && !reg.test(_.get(data, rule[i].name, ''))) { this.error = rule[i].errorMsg; return false }
          break
        case 'notnull':
          if (_.get(data, rule[i].name) == null || _.get(data, rule[i].name).length < 1) {
            this.error = rule[i].errorMsg
            return false
          }
          break
      }
    }
    return true
  },
  isNumber: function(checkVal) {
    var reg = /^-?[1-9][0-9]?.?[0-9]*$/
    return reg.test(checkVal)
  }

}
